package com.zmn.oms.dubbo.impl.quotation;

import com.alibaba.fastjson.JSON;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.business.interfaces.orderamount.OrderAmountBService;
import com.zmn.oms.common.dio.quotation3.ArtificialPromDRO;
import com.zmn.oms.common.dio.quotation3.ArtificialPromListQuery;
import com.zmn.oms.common.dio.quotation3.CheckReportDIO;

import com.zmn.oms.common.dio.quotation3.MaintainFaultOptionQuery;
import com.zmn.oms.common.dio.quotation3.MaintainOptionDRO;
import com.zmn.oms.common.dro.quotation3.*;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.dubbo.interfaces.quotation.Quotation3OrderWorkListRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.entity.quotation3.OrderAcceptMaintainItem;
import com.zmn.oms.services.interfaces.quotation3.OrderAcceptMaintainItemService;
import com.zmn.oms.zmn.business.interfaces.quotation3.OrderQuotation3ListBService;
import com.zmn.oms.zmn.business.interfaces.work.masterwork.MasterWorkListBService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author sunlife
 * @date:2021/12/2 6:13 下午
 * description:
 */
@Slf4j
@Service(loadbalance = "roundrobin", timeout = 20000, retries = 0, version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class Quotation3OrderWorkListRemoteServiceImpl implements Quotation3OrderWorkListRemoteService {

    @Autowired
    OrderQuotation3ListBService orderQuotation3ListBService;
    @Autowired
    OrderAmountBService orderAmountBService;
    @Autowired
    OrderAcceptMaintainItemService orderAcceptMaintainItemService;
    @Autowired
    MasterWorkListBService masterWorkListBService;

    @Override
    public ResponseDTO<Quotation3WorkDetailDRO> getWorkDetail(Long orderId, Long workId) {
        try {
            if (NumberUtil.isNullOrZero(orderId) || NumberUtil.isNullOrZero(workId)) {
                return ResponseDTO.fail("订单ID不能为空");
            }
            Quotation3WorkDetailDRO workDetailDRO = masterWorkListBService.getQuotation3WorkDetail(orderId, workId);
            return ResponseDTO.success(workDetailDRO);
        } catch (OmsBaseException e) {
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("获取订单详情失败", e);
            return ResponseDTO.fail("获取订单详情失败，请稍后重试");
        }
    }

    @Override
    public ResponseDTO<Integer> getDiscount(@Valid CheckReportDIO checkReportDIO) {
        log.info("提交检测报告 {}", JSON.toJSONString(checkReportDIO));
        try {
            Integer discount = orderQuotation3ListBService.getDiscount(checkReportDIO);
            return ResponseDTO.success(discount);
        } catch (OmsBaseException e) {
            log.error("提交检测报告报错", e);
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        }
    }

    @Override
    public ResponseDTO<UserConfirmedFaultInfoDRO> getUserConfimedFaultInfo(Long orderId, Long workId) {
        log.info("[{}]获取故障确认信息", orderId);
        try {
            UserConfirmedFaultInfoDRO userConfirmedFaultInfoDRO = orderQuotation3ListBService.getUserConfimedFaultInfo(orderId, workId);
            return ResponseDTO.success(userConfirmedFaultInfoDRO);
        } catch (OmsBaseException e) {
            log.error("获取故障确认信息报错", e);
            return ResponseDTO.fromErrorCodeTable(e.getCode());
        }
    }

    @Override
    public ResponseDTO<CheckReportDRO> getCheckReport(Long orderId, Long workId) {
        log.info("[{}]获取检测报告", orderId);
        try {
            CheckReportDRO checkReportDRO = orderQuotation3ListBService.getCheckReport(orderId, workId);
            return ResponseDTO.success(checkReportDRO);
        } catch (OmsBaseException e) {
            log.error("获取检修报告报错", e);
            return ResponseDTO.fromErrorCodeTable(e.getCode());
        }
    }

    @Override
    public ResponseDTO<OrderAmountDRO> refreshOrderAmount(Long orderId, Long workId, Integer discountId) {
        try {
            OrderAmountDRO orderAmountDRO = orderQuotation3ListBService.refreshOrderAmount(orderId, workId, discountId);
            return ResponseDTO.success(orderAmountDRO);
        } catch (OmsBaseException e) {
            log.error("重新获取订单价格失败", e);
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        }
    }

    @Override
    public ResponseDTO<OrderAmountDRO> refreshOrderAmount2(Long orderId, Long workId, Integer discountAmount) {
        try {
            OrderAmountDRO orderAmountDRO = orderQuotation3ListBService.refreshOrderAmount2(orderId, workId, discountAmount);
            return ResponseDTO.success(orderAmountDRO);
        } catch (OmsBaseException e) {
            log.error("重新获取订单价格失败", e);
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        }
    }

    @Override
    public ResponseDTO<EngineerDiscountDRO> getOrderEngineerDiscount(Long orderId, Long workId) {
        try {
            EngineerDiscountDRO engineerDiscountDRO = orderAmountBService.getOrderEngineerDiscount(orderId, workId);
            return ResponseDTO.success(engineerDiscountDRO);
        } catch (OmsBaseException e) {
            log.error("获取订单优惠失败", e);
            return ResponseDTO.fromErrorCodeTable(e.getCode());
        } catch (Exception e) {
            log.error("获取订单优惠失败", e);
            return ResponseDTO.fail("获取工程师优惠失败，请稍后重试");
        }
    }

    @Override
    public ResponseDTO<List<OrderAcceptMaintainItemDRO>> listOrderAcceptMaintainItemByOrderIdAndWorkId(Long orderId, Long workId) {
        log.info("查询工程师验收检测项目入参：[{}]-[{}]", orderId, workId);
        try {
            List<OrderAcceptMaintainItem> list = orderAcceptMaintainItemService.listByOrderIdAndWorkId(orderId, workId);
            List<OrderAcceptMaintainItemDRO> resultList = BeanMapper.mapList(list, OrderAcceptMaintainItemDRO.class);
            log.info("[{}]查询工程师验收检测项目返回：【{}】", orderId, JSON.toJSONString(resultList));
            return ResponseDTO.success(resultList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取订单验收检测项目", e);
            return ResponseDTO.fail("获取订单验收检测项目，请稍后重试");
        }
    }

    @Override
    public ResponseDTO<List<ArtificialPromDRO>> listArtificialProm(ArtificialPromListQuery query) {
        try {
            List<ArtificialPromDRO> promDROList = orderQuotation3ListBService.listArtificialProm(query);
            return ResponseDTO.success(promDROList);
        } catch (OmsBaseException e) {
            log.error("获取人工优惠信息列表失败", e);
            return ResponseDTO.fromErrorCodeTable(e.getCode());
        } catch (Exception e) {
            log.error("获取人工优惠信息列表失败", e);
            return ResponseDTO.fail("获取人工优惠信息列表失败，请稍后重试");
        }
    }

    @Override
    public ResponseDTO<List<MaintainOptionDRO>> listMaintainOptionByMfoQuery(MaintainFaultOptionQuery maintainFaultOptionQuery) {
        try {
            List<MaintainOptionDRO> list = orderQuotation3ListBService.listMaintainOptionByMfoQuery(maintainFaultOptionQuery);
            return ResponseDTO.success(list);
        } catch (OmsBaseException e) {
            log.error("根据故障id和检测项目id选项id返回应该选择的选项失败", e);
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            log.error("根据故障id和检测项目id选项id返回应该选择的选项失败", e);
            return ResponseDTO.fail("根据故障id和检测项目id选项id返回应该选择的选项，请稍后重试");
        }
    }

    @Override
    public ResponseDTO<Integer> processArtificial(Long orderId,Long workId,Integer discountAmount) {
        try {
            Integer processArtificial = orderQuotation3ListBService.processArtificial(orderId,workId,discountAmount);
            return ResponseDTO.success(processArtificial);
        } catch (OmsBaseException e) {
            log.error("处理优惠失败", e);
            return ResponseDTO.fromErrorCodeTable(e.getCode());
        } catch (Exception e) {
            log.error("处理优惠失败", e);
            return ResponseDTO.fail("处理优惠失败，请稍后重试");
        }
    }
}
